//
//  SEGQueryGoodsIdel_m
//  ExcellentHome
//
//  Created by 刘山 on 16/9/5.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "SEGQueryGoodsIdelModel.h"
#if __has_include(<Commom/SEGServerManager.h>)
#import <Commom/SEGServerManager.h>
#else
#import "SEGServerManager.h"
#endif
#if __has_include(<Commom/SEGUtils.h>)
#import <Commom/SEGUtils.h>
#else
#import "SEGUtils.h"
#endif
#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif
#if __has_include(<SEGPlatform/SEGBussinessConstHeader.h>)
#import <SEGPlatform/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif

@implementation SEGQueryGoodsIdelModel

+ (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    return [[self alloc] initWithDictionary:dictionary];
}

- (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dictionary];
        //设置layout
        [self saveLayout];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString*)key
{
}

- (void)setValue:(id)value forKey:(NSString*)key
{
    if ([key isEqualToString:@"comment"]) {
        if ([value isKindOfClass:[NSArray class]]) {
            NSMutableArray* _arrM = [NSMutableArray array];
            for (NSDictionary* dict in value) {
                SEGSharedNeihborhoodCommentModel* model = [SEGSharedNeihborhoodCommentModel initWithDictionary:dict];
                [_arrM addObject:model];
            }
            _comment = _arrM.copy;
        }
        return;
    }
    
    [super setValue:value forKey:key];
}


- (void)setImg:(NSString*)img
{
    if ([img isEqual:nil]) {
        img = @"";
    }
    _img = img;
    
    _img1 = [img copy];
    
    NSArray* arr = @[];
    
    NSMutableArray* arrM = [NSMutableArray array];
    NSMutableArray* arrM2 = [NSMutableArray array];
    if ([_img rangeOfString:@","].length > 0) {
        arr = [_img componentsSeparatedByString:@","];
    }
    else if (_img.length > 0) {
        [arrM addObject:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, _img]];
    }
    
    [arr enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL* _Nonnull stop) {
        
        if ([obj length] > 0) {
            [arrM addObject:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, obj]];
            [arrM2 addObject:obj];
        }
        
    }];
    
    self.quizPicArr = arrM;
    self.noRootPicArr = [arrM2 copy];
    
    if (arrM.count > 0) {
        _img = arrM.firstObject;
    }
}

- (instancetype)init
{
    if (self == [super init]) {
        _img1 = @"";
        _changeCode = @"0";
        _changeStatus = @"";
    }
    return self;
}

-(void)setChangeCode:(NSString *)changeCode{
    _changeCode = [ValueToString(changeCode)copy];
}

-(void)setChangeStatus:(NSString *)changeStatus{
    _changeStatus = [ValueToString(changeStatus)copy];
}


- (void)saveLayout
{
    // 控件数据模型创建
    SEGcustomContentViewModel *data = [[SEGcustomContentViewModel alloc] init];
    data.comment = ValueToString(_commentCount);
    data.widgetWidth = LYScreenWidth() - kNeighbourTextLeftPadding - 15;//宽度
    SEGMarketDetailViewModel *marketDetailDataSource = [[SEGMarketDetailViewModel alloc] init];
    marketDetailDataSource.title = _title;
    marketDetailDataSource.content = _exp;
    marketDetailDataSource.isChange = _changeCode;
    marketDetailDataSource.widgetWidth = data.widgetWidth;//宽度;
    marketDetailDataSource.price = [SEGBasicBussinessEnums getShowPriceWithMarketType:[_type integerValue] salePrice:_salePrice rent:ValueToString(_rent) unitName:_unitName];
    marketDetailDataSource.imageContent = _quizPicArr;
    marketDetailDataSource.isGift = ([_type integerValue] == SEGEnumIdelExchange_Gift);//是否为赠送
    data.marketDetailDataSource = marketDetailDataSource;
    data.isDetailView = [_isDetailView isEqualToString:@"1"];
    _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_MARKETDETAIL];
}

// 转换集市模型的type
- (NSString *)convertToObjType {
    SEGEnumIdelExchange IdelExchange = [self.type integerValue];
    NSString* objType = [SEGBasicBussinessEnums getSEGEnumIdelExchange:IdelExchange];
    return ValueToString(objType) ?: @"";
}

@end
